package com.sgs.carbon.starter.security.core.aop;

import com.sgs.carbon.common.exception.enums.GlobalErrorCodeConstants;
import com.sgs.carbon.common.exception.util.ServiceExceptionUtil;
import com.sgs.carbon.starter.security.core.annotations.PreAuthenticated;
import com.sgs.carbon.starter.security.core.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

/**
 * {@link PreAuthenticated} 切面处理类
 *
 * @author Dave Liu
 * @date 2021-10-01
 */
@Slf4j
@Aspect
public class PreAuthenticatedAspect {

    @Around("@annotation(preAuthenticated)")
    public Object around(ProceedingJoinPoint joinPoint, PreAuthenticated preAuthenticated) throws Throwable {
        if (SecurityUtils.getLoginUser() == null) {
            throw ServiceExceptionUtil.exception(GlobalErrorCodeConstants.UNAUTHORIZED);
        }
        return joinPoint.proceed();
    }

}
